import json
import random
import re
import time

import execjs
import requests

import cookies_pool
from get_new_cookies import get_new_skey


def get_page_detil(book_id,page_num,session):
    headers = {
        "authority": "weread.qq.com",
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN,zh;q=0.9",
        "content-type": "application/json;charset=UTF-8",
        "origin": "https://weread.qq.com",
        "referer": "https://weread.qq.com/web/reader/1a1321b07208f4cb1a152afkecc32f3013eccbc87e4b62e",
        "sec-ch-ua": "\"Chromium\";v=\"116\", \"Not)A;Brand\";v=\"24\", \"Google Chrome\";v=\"116\"",
        "sec-ch-ua-mobile": "?1",
        "sec-ch-ua-platform": "\"Android\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "user-agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.36"
    }
    cookies = cookies_pool.cookies
    cookies['wr_skey'] = get_new_skey()
    js_code = open('b.js',encoding='utf8')
    js_codeDe = open('decrypt.js',encoding='utf8')
    js = execjs.compile(js_code.read())
    jsDe = execjs.compile(js_codeDe.read())
    ps = js.call('getMd5',int(time.time()))
    cid = page_num #input('输入章节id:')
    b,c = js.call('getUrlParam',book_id,cid)
    r = session.get('https://weread.qq.com/web/reader/' + b+ 'k' + c)
    psvts = re.findall(',"psvts":"(.*?)","pclts":0,', r.text)[0]
    # print(r.text)
    data = js.call('getPyload',book_id,cid,ps,psvts)

    response01 = session.post('https://weread.qq.com/web/book/chapter/e_0', headers=headers, cookies=cookies,json=data)
    response02 = session.post('https://weread.qq.com/web/book/chapter/e_1', headers=headers,cookies=cookies, json=data)
    response03 = session.post('https://weread.qq.com/web/book/chapter/e_2', headers=headers,cookies=cookies, json=data)
    response04 = session.post('https://weread.qq.com/web/book/chapter/e_3', headers=headers, cookies=cookies,json=data)
    # print(response01.text)
    text = response01.text[32:] + response02.text[32:] + response04.text[32:]


    content=jsDe.call('decrypt',text)
    print(content[:30])
    return content


    # book_content.insertOne({"_id":id,"book_id": book_id, 'chapter_num': page_num,'content':content})

if __name__=='__main__':
    # book_id_db = utils.mongo_manager(collect_name="book_id")
    # book_content=utils.mongo_manager(collect_name="book_content")
    # book_id_info = book_id_db.find_one_and_update({"bookStatus": 1}, {"state": 2})
    # # print(book_id_info)
    # book_id= book_id_info.get('_id')
    # page_list=book_id_info.get('chapter_num')
    # print(book_id,page_list)
    session = requests.session()
    # page_list=[i for i in range(1,68)]
    # print(page_list)
    # for i in page_list:
    #     get_page_detil(3300069598, i,session)
    #     time.sleep(random.randint(5, 10) )
    get_page_detil(721178, 273,session)
